﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Jobs_MVC_3_1.Models;
using PagedList;

namespace Jobs_MVC_3_1.Controllers
{ 
    
    public class JobPostingController : Controller
    {
        private JobsEntities db = new JobsEntities();

        //
        // GET: /JobPosting/

        public ViewResult Index(int? page, string sortOrder)
        {
            if (String.IsNullOrEmpty(sortOrder))
                sortOrder = "PostingId desc";

            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "PostingId desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "Date desc" : "Date";

            if (Request.HttpMethod == "GET")
            {
               
            }
            else
            {
                page = 1;
            }

           // var jobpostings = db.JobPostings.Include(j => j.ClientCompany).Include(j => j.ClosingReason).Include(j => j.Country).Include(j => j.EducationLevel).Include(j => j.JobType).Include(j => j.Nationality).Include(j => j.JobStatu);

            var jobpostings =   from s in db.JobPostings 
                                select s;
            switch (sortOrder)
            {
                case "PostingId desc":
                    jobpostings = jobpostings.OrderByDescending(s => s.PostingId );
                    break;
                case "ClientCompany":

                    jobpostings = from s in db.JobPostings //jobpostings.OrderBy(s => s.ClientCompany);
                                  orderby s.ClientCompany descending
                                  select s;

                    break;
                default:
                    jobpostings = jobpostings.OrderByDescending(s => s.PostingId);
                    break;
            }

            int pageSize = 10;
            int pageIndex = (page ?? 1) - 1;

            
            //jobpostings = jobpostings.OrderBy(j => j.PostingId);
            return View(jobpostings.ToPagedList(pageIndex, pageSize));
           // return View(db.JobPostings);
         }

        //
        // GET: /JobPosting/Details/5

        public ViewResult Details(int id)
        {
            JobPosting jobposting = db.JobPostings.Find(id);
            return View(jobposting);
        }

        //
        // GET: /JobPosting/Create

        public ActionResult Create()
        {
            ViewBag.CompanyID = new SelectList(db.ClientCompanies, "CompanyID", "CompanyName");
            ViewBag.ClosingReasonID = new SelectList(db.ClosingReasons, "ReasonID", "ReasonName");
            ViewBag.CountryId = new SelectList(db.Countries, "CountryID", "CountryName");
            ViewBag.EducationLevelID = new SelectList(db.EducationLevels, "EducationLevelID", "EducationLevelName");
            ViewBag.JobTypeID = new SelectList(db.JobTypes, "JobTypeID", "JobTypeName");
            ViewBag.NationalityID = new SelectList(db.Nationalities, "NationalityID", "NationalityName");
            ViewBag.Status = new SelectList(db.JobStatus1, "JobStatusID", "JobStatusName");
            return View();
        } 

        //
        // POST: /JobPosting/Create

        [HttpPost]
        public ActionResult Create(JobPosting jobposting)
        {
            if (ModelState.IsValid)
            {
                db.JobPostings.Add(jobposting);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.CompanyID = new SelectList(db.ClientCompanies, "CompanyID", "CompanyName", jobposting.CompanyID);
            ViewBag.ClosingReasonID = new SelectList(db.ClosingReasons, "ReasonID", "ReasonName", jobposting.ClosingReasonID);
            ViewBag.CountryId = new SelectList(db.Countries, "CountryID", "CountryName", jobposting.CountryId);
            ViewBag.EducationLevelID = new SelectList(db.EducationLevels, "EducationLevelID", "EducationLevelName", jobposting.EducationLevelID);
            ViewBag.JobTypeID = new SelectList(db.JobTypes, "JobTypeID", "JobTypeName", jobposting.JobTypeID);
            ViewBag.NationalityID = new SelectList(db.Nationalities, "NationalityID", "NationalityName", jobposting.NationalityID);
            ViewBag.Status = new SelectList(db.JobStatus1, "JobStatusID", "JobStatusName", jobposting.Status);
            return View(jobposting);
        }
        
        //
        // GET: /JobPosting/Edit/5
 
        public ActionResult Edit(int id)
        {
            JobPosting jobposting = db.JobPostings.Find(id);
            ViewBag.CompanyID = new SelectList(db.ClientCompanies, "CompanyID", "CompanyName", jobposting.CompanyID);
            ViewBag.ClosingReasonID = new SelectList(db.ClosingReasons, "ReasonID", "ReasonName", jobposting.ClosingReasonID);
            ViewBag.CountryId = new SelectList(db.Countries, "CountryID", "CountryName", jobposting.CountryId);
            ViewBag.EducationLevelID = new SelectList(db.EducationLevels, "EducationLevelID", "EducationLevelName", jobposting.EducationLevelID);
            ViewBag.JobTypeID = new SelectList(db.JobTypes, "JobTypeID", "JobTypeName", jobposting.JobTypeID);
            ViewBag.NationalityID = new SelectList(db.Nationalities, "NationalityID", "NationalityName", jobposting.NationalityID);
            ViewBag.Status = new SelectList(db.JobStatus1, "JobStatusID", "JobStatusName", jobposting.Status);
            return View(jobposting);
        }

        //
        // POST: /JobPosting/Edit/5

        [HttpPost]
        public ActionResult Edit(JobPosting jobposting)
        {
            if (ModelState.IsValid)
            {
                db.Entry(jobposting).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CompanyID = new SelectList(db.ClientCompanies, "CompanyID", "CompanyName", jobposting.CompanyID);
            ViewBag.ClosingReasonID = new SelectList(db.ClosingReasons, "ReasonID", "ReasonName", jobposting.ClosingReasonID);
            ViewBag.CountryId = new SelectList(db.Countries, "CountryID", "CountryName", jobposting.CountryId);
            ViewBag.EducationLevelID = new SelectList(db.EducationLevels, "EducationLevelID", "EducationLevelName", jobposting.EducationLevelID);
            ViewBag.JobTypeID = new SelectList(db.JobTypes, "JobTypeID", "JobTypeName", jobposting.JobTypeID);
            ViewBag.NationalityID = new SelectList(db.Nationalities, "NationalityID", "NationalityName", jobposting.NationalityID);
            ViewBag.Status = new SelectList(db.JobStatus1, "JobStatusID", "JobStatusName", jobposting.Status);
            return View(jobposting);
        }

        //
        // GET: /JobPosting/Delete/5
 
        public ActionResult Delete(int id)
        {
            JobPosting jobposting = db.JobPostings.Find(id);
            return View(jobposting);
        }

        //
        // POST: /JobPosting/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            JobPosting jobposting = db.JobPostings.Find(id);
            db.JobPostings.Remove(jobposting);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}